import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//全局引入axios
// 引用axios，并设置基础URL为后端服务api地址
import axios from 'axios'
//生产环境应该对应网关的地址+端口,测试对应的是easymock的地址
axios.defaults.baseURL = 'http://127.0.0.1:1299/services'
// 将API方法绑定到全局  /plat/login
Vue.prototype.$http = axios //组件中this.$http 使用axios  this.$http
Vue.config.productionTip = false

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes:routes
})

//每次路由之前
//sessionStorage前端的session
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user'); //session中移除user
  }
  let user = JSON.parse(sessionStorage.getItem('user')); //从session中获取user
  if (!user && to.path != '/login') {//如果session中没有user并且跳转的路径不是登录页面
    next({ path: '/login' }) //跳转到登录页面
  } else {
    next() //放行
  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router, //路由的配置
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

